import 'dart:convert';

void main(List<String> args) {
  String str = '{"name": "张三", "age": 20}';
  print(str); // {"name": "张三", "age": 20} 有引号
  print(jsonDecode(str)); // {name: 张三, age: 20}
  Map<String, dynamic> map = jsonDecode(str);
  print(map); // {name: 张三, age: 20}

  People p = People.fromToJson(map);
  print(p.name);
  print(p.age);
}

class People {
  String? name;
  int? age;

  People({this.name, this.age});

  //factory 是类中的工厂关键字相当于在类上添加了一个静态方法
  factory People.fromToJson(Map<String, dynamic> json) {
    return People(name: json['name'], age: json['age']);
  }
}
